package com.wave.gate.controller;

import com.alibaba.fastjson2.JSON;
import com.wave.gate.constant.CommonConstants;
import com.wave.gate.domain.PassRecord;
import com.wave.gate.enums.RedisKeyEnum;
import com.wave.gate.netty.FaceServerHandler;
import com.wave.gate.service.IPassRecordService;
import com.wave.gate.util.GuavaCache;
import com.wave.gate.util.VoicePlayerUtil;
import com.wave.gate.util.NorcoAPIUtil;
import com.wave.gate.vo.AiAlarmVO;
import com.wave.gate.vo.GatePassSetVo;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import static com.wave.gate.util.GuavaCache.AI_CACHE;
import static com.wave.gate.util.GuavaCache.DOOR_MODE_CACHE;

/**
 * @author TanLei
 * @className：controller
 * @description：TODO
 * @date 2023/4/25 17:42
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class NorcoController {

    @Value("${electronicBalance.index}")
    public int electronicBalanceIndex;

    @Autowired
    IPassRecordService passRecordService;

    //测试结果
    @RequestMapping("/test")
    public String test() throws InterruptedException {
//        long a1 = System.currentTimeMillis();
//        String a = "手机二维码信息:" + NorcoAPIUtil.getDeviceData(1) + "===token二维码信息:" + NorcoAPIUtil.getDeviceData(2);
//        long a2 = System.currentTimeMillis();
//        System.out.println(a2 - a1);
//        String b = "\r手机二维码信息:" + NorcoAPIUtil.getDeviceData(3) + "===token二维码信息:" + NorcoAPIUtil.getDeviceData(4);
//        long a3 = System.currentTimeMillis();
//        System.out.println(a3 - a2);
        String c = "\r重量：" + NorcoAPIUtil.getDeviceWeightData(1);
        long a4 = System.currentTimeMillis();
//        System.out.println(a4 - a3);
        String d = "\r重量：" + NorcoAPIUtil.getDeviceWeightData(2);
//        long a5 = System.currentTimeMillis();
//        System.out.println(a5 - a4);
        return c + d;
    }

    /**
     * 电子秤归零
     * @param type 1：代表1号电子秤，5：代表2号电子秤
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/balanceZero")
    public String balanceZero(int type) throws InterruptedException {
        if(type == 1){
            String zeroorder = CommonConstants.ZEROORDER1_1;
            if(electronicBalanceIndex == 2){
                zeroorder = CommonConstants.ZEROORDER2_1;
            }
            NorcoAPIUtil.electronicBalanceSendData(CommonConstants.COM_NODE0, zeroorder);
        }
        if(type == 5){
            String zeroorder = CommonConstants.ZEROORDER1_2;
            if(electronicBalanceIndex == 2){
                zeroorder = CommonConstants.ZEROORDER2_2;
            }
            NorcoAPIUtil.electronicBalanceSendData(CommonConstants.COM_NODE0, zeroorder);
        }
        return NorcoAPIUtil.getDeviceData(5);
    }

    /**
     * 电子秤零点标定
     * @param type 1：代表1号电子秤，5：代表2号电子秤
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/balanceAdjustZero")
    public String balanceAdjustZero(int type) throws InterruptedException {
        if(type == 1){
            NorcoAPIUtil.electronicBalanceSendData(CommonConstants.COM_NODE0, CommonConstants.ZEROADJUST1);
        }
        if(type == 5){
            NorcoAPIUtil.electronicBalanceSendData(CommonConstants.COM_NODE0, CommonConstants.ZEROADJUST2);
        }
        return NorcoAPIUtil.getDeviceData(5);
    }

    /**
     * 电子秤归零
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/player")
    public String player(int type) throws Exception {
        VoicePlayerUtil.voicePlay(type);
        return "播放成功";
    }

    @RequestMapping("/kkk")
    public void kkk(int type) {
        String key = RedisKeyEnum.KEY_AI_ALARM.getCode();
        if(type == 1){
            AI_CACHE.put(key, "尾随-入口-门外告警");
        }
        if(type == 2){
            AI_CACHE.put(key, "尾随-入口-门内告警");
        }
        if(type == 3){
            AI_CACHE.put(key, "尾随-出口-门外告警");
        }
        if(type == 4){
            AI_CACHE.put(key, "尾随-出口-门内告警");
        }
        if(type == 5){
            AI_CACHE.put(key, "跨越");
        }
        if(type == 6){
            AI_CACHE.put(key, "弯腰");
        }
        if(type == 7){
            AI_CACHE.put(key, "下蹲");
        }
        if(type == 8){
            AI_CACHE.put(key, "举手过肩");
        }
        if(type == 9){
            key = RedisKeyEnum.KEY_AI_GY.getCode();
            AI_CACHE.put(key, "已穿访客工衣");
        }
    }

    @RequestMapping("/hc")
    public String hc() {
        /*log.info("通道设备ip：{}", GuavaCache.PASSSET_CACHE.asMap());
        log.info("手机/token重量误差集合：{}", GuavaCache.MOBILE_TOKEN_WEIGHTLIST_CACHE.asMap());
        log.info("通道内是否有人集合：{}", GuavaCache.HASHUMAN_CACHE.asMap());
        log.info("闸机模式：{}", GuavaCache.DOOR_MODE_CACHE.asMap());
        log.info("5s内同一个告警，只缓存一次，此缓存作为校验：{}", GuavaCache.CHECK_MODELPATH_CACHE.asMap());
        log.info("AI告警缓存：{}", GuavaCache.AI_CACHE.asMap());*/
        String result = "通道设备ip：" + GuavaCache.PASSSET_CACHE.asMap() + "<br/>" +
        "手机/token重量误差集合：" + GuavaCache.MOBILE_TOKEN_WEIGHTLIST_CACHE.asMap() + "<br/>" +
        "通道内是否有人集合：" + GuavaCache.HASHUMAN_CACHE.asMap() + "<br/>" +
        "闸机模式：" + GuavaCache.DOOR_MODE_CACHE.asMap() + "<br/>" +
        "5s内同一个告警，只缓存一次，此缓存作为校验：" + GuavaCache.CHECK_MODELPATH_CACHE.asMap() + "<br/>" +
        "AI告警缓存：" + GuavaCache.AI_CACHE.asMap();
        return result;
    }

    @RequestMapping("/clear/hc")
    public String clearHc() {
        GuavaCache.PASSSET_CACHE.invalidateAll();
        log.info("清理通道设备ip缓存成功");
        GuavaCache.MOBILE_TOKEN_WEIGHTLIST_CACHE.invalidateAll();
        log.info("清理手机/token重量误差集合缓存成功");
        GuavaCache.HASHUMAN_CACHE.invalidateAll();
        log.info("清理通道内是否有人集合缓存成功");
        GuavaCache.DOOR_MODE_CACHE.invalidateAll();
        log.info("清理闸机模式缓存成功");
        GuavaCache.CHECK_MODELPATH_CACHE.invalidateAll();
        log.info("清理5s内同一个告警，只缓存一次，此缓存作为校验缓存成功");
        GuavaCache.AI_CACHE.invalidateAll();
        log.info("清理AI告警缓存缓存成功");
        return "清理成功";
    }

    @RequestMapping("/record")
    public String record() {
        return JSON.toJSONString(passRecordService.list());
    }

    @RequestMapping("/clients")
    public String clients() {
        for (Map.Entry<String, Channel> client : FaceServerHandler.channels.entrySet()) {
            log.info("人脸机,key:" + client.getKey() + ",value:" + client.getValue());
        }
        return JSON.toJSONString(FaceServerHandler.channels);
    }
}
